﻿using System;
using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using System.Drawing;
using Close.controls.MenuPanels;
using System.Windows.Forms;

namespace Close.Controls
{
    class SelectLevelTemplate: FluidTemplate
    {
        protected override void InitControl()
        {
            base.InitControl();

            this.Bounds = new Rectangle(0, 0, 240, 46);
            FluidLabel l;

            //FluidTextBox tb = new FluidTextBox("", 3, 17, 228, 24);
            //tb.LineAlignment = StringAlignment.Near;
            //tb.Alignment = StringAlignment.Near;
            //tb.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
            //tb.Font = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Bold);
            //tb.ShowBorder = false;
            //tb.BackColor = Color.Transparent;
            ////tb.TextChanged += new EventHandler(tb_TextChanged);
            ////tb.FocusChanged += new EventHandler(tb_FocusChanged);
            ////tb.Bind += new EventHandler(tb_Bind);
            //valueTb = tb;
            //Controls.Add(tb);

            l = new FluidLabel("", 3, 3, 50, 20);
            l.LineAlignment = StringAlignment.Near;
            l.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
            l.Font = new Font(FontFamily.GenericSansSerif, 7f, FontStyle.Regular);
            //l.ForeColor = Theme.Current.ListSecondaryForeColor;
            lblId = l;
            Controls.Add(l);

            lblState = new FluidLabel("", 56, 3, 100, 20);
            lblState.LineAlignment = StringAlignment.Near;
            lblState.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
            lblState.Font = new Font(FontFamily.GenericSansSerif, 12f, FontStyle.Regular);
            //lblState.ForeColor = Theme.Current.ListSecondaryForeColor;
            Controls.Add(lblState);

            lblPlayedTime = new FluidLabel("", 56, 26, 100, 20);
            lblPlayedTime.LineAlignment = StringAlignment.Center;
            lblPlayedTime.Font = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Bold);
            Controls.Add(lblPlayedTime);

            lblDate = new FluidLabel("", 106, 26, 120, 20);
            lblDate.LineAlignment = StringAlignment.Near;
            lblDate.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
            lblDate.Font = new Font(FontFamily.GenericSansSerif, 12f, FontStyle.Regular);
            //lblState.ForeColor = Theme.Current.ListSecondaryForeColor;
            Controls.Add(lblDate);
        }


        LevelListBox ListBox { get { return Parent as LevelListBox; } }

        void addBtnClick(object sender, EventArgs e)
        {
            AddItem();
        }

        void removeBtnClick(object sender, EventArgs e)
        {
            RemoveItem();

        }

        private void AddItem()
        {
            LevelListBoxItem item = Item as LevelListBoxItem;
            if (item != null)
            {
                //item.IsUsed = true;
                InvalidateItem();
            }
        }

        private void RemoveItem()
        {
            LevelListBoxItem item = Item as LevelListBoxItem;
            if (item != null)
            {
                //item.IsUsed = false;
                InvalidateItem();
            }
        }

        public void InvalidateItem()
        {
            ListBox.InvalidateItem(ItemIndex);
            //            ListBox.Invalidate();
        }


        protected override void OnItemUpdate(object value)
        {
            base.OnItemUpdate(value);
            LevelListBoxItem item = value as LevelListBoxItem;
            if (item != null)
            {
                //*//titleLabel.Text = item.Title;
                //removeBtn.Visible = item.IsUsed;
                //addBtn.Visible = !item.IsUsed;
                //titleLabel.ForeColor = !item.IsUsed ? Color.Empty : Theme.Current.GrayTextColor;
            }
            else
            {
                //*//titleLabel.Text = "";
                //*//removeBtn.Visible = false;
                //*//addBtn.Visible = false;
            }
        }

        private FluidLabel lblId;
        private FluidLabel lblPlayedTime;
        private FluidLabel lblState;
        private FluidLabel lblDate;

        protected override void OnBindValue(object value)
        {
            if (value is string) //TODO: ugly - fix (split) parent
                lblId.Text = value.ToString();
            else if (value is GameDetail)
            {
                GameDetail item = value as GameDetail;
                lblId.Text = item.Id.ToString();

                lblPlayedTime.Text = item.TimeElapsedString;                
                lblState.Text = item.StateString;
                lblDate.Text = item.LastPlayedDateString;
            }
            else
                throw new NotImplementedException("combo binding");

        }
    }
}
